********************************************************************************
* Purpose: Creates survey outcomes and merge the survey to sample from birth records
********************************************************************************


do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

*Merge with first wave of survey
use "D:\Data\workdata\708296\SPOR\baseline\work\survey_internal_wide.dta", clear
save "$work\SPOR_baseline.dta", replace
gen baseline=1

tab cohort

//keep selected survey variables
keep cohort  pnr_barn pnr_f pnr_m atrisk asq* mabisc* pss* who* relsat* stimact* childact* daycare* baseline nonmiss_asq_f nonmiss_asq_m pss_ps_f pss_ps_m pss_lps_f pss_lps_m conflict* drinking* smoking* csrq*

gen csi_f=relsat_1_f+relsat_2_f+relsat_3_f+relsat_4_f+relsat_5_f
gen csi_m=relsat_1_m+relsat_2_m+relsat_3_m+relsat_4_m+relsat_5_m

rename (relsat_4_f relsat_4_m) (relsat_f relsat_m)

keep pnr_barn pnr_f pnr_m     pss_total_f pss_total_m pss_total_own_f pss_total_own_m mabisc_total_m mabisc_total_f asq_3yr_total_m asq_3yr_total_f asq_2yr_total_m asq_2yr_total_f asq_9mo_total_m asq_9mo_total_f stimact*   cohort baseline  relsat_f relsat_m csi_f csi_m nonmiss_asq_f nonmiss_asq_m pss_ps_f pss_ps_m pss_lps_f pss_lps_m atrisk who* conflict* drinking* smoking* csrq*

rename pnr_barn pnr
rename pnr_f cprf
rename pnr_m cprm

merge 1:1 pnr  using  "$work\mfr_kids", keepusing(mfr_muni)
keep if _merge==3 // only those spor kids with a muni can be used later; only spor kids from the register data are kept here
drop _merge
save "$work\SPOR.dta", replace

